<?php
/**
 * Http服务
 *
 * @author		黑冰 <954650386@qq.com>
 */

namespace waf\server;

use \waf\Waf;
use \waf\log\WLog;
use \waf\server\BaseServer;
use \Yaf;
use \waf\client\monitor\WMonitorClient;

class HttpServer extends BaseServer
{
    public function init()
    {
        WLog::logSystem("in", __FILE__, __LINE__, __FUNCTION__);
        $this->strServerType = 'http';
        parent::init();
        WLog::logSystem("out", __FILE__, __LINE__, __FUNCTION__);
    }

    public function onRequest($objRequest, $objResponse)
    {
        WLog::logInfo("in:".WLog::toJson($objRequest->server['request_uri']), __FILE__, __LINE__, __FUNCTION__);
        ob_start();

        WMonitorClient::tick('HttpServer_onRequest', str_replace('/', '_', $objRequest->server['request_uri']));
        $success    = true;
        try {
            $requestObj = new \Yaf_Request_Http($objRequest->server['request_uri']);
            $this->setParams($requestObj, get_object_vars($objRequest));
            $this->objYaf->getDispatcher()->dispatch($requestObj);
        } catch (\Throwable $e) {
            $success = false;
            WLog::logError("yaf_exception:".WLog::toJson(print_r($e, true)), __FILE__, __LINE__, __FUNCTION__);
        }
        WMonitorClient::report('HttpServer_onRequest', str_replace('/', '_', $objRequest->server['request_uri']), $success);

        $result = ob_get_contents();
        ob_end_clean();
        $objResponse->end($result);
    }
}
?>